package com.monkeyboy.spring.pattern.jdk;


import com.monkeyboy.spring.annotation.CustomAnno;
import com.monkeyboy.spring.dao.UserDao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Author Gavin
 * @date 2021.02.21 13:37
 */
public class MyInvocationHandler implements InvocationHandler {
	UserDao userDao;

	public MyInvocationHandler() {
		this.userDao = new UserDaoImpl();
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("conn db");
		CustomAnno annotation = method.getAnnotation(CustomAnno.class);
		System.out.println("代理的方法名："+method.getName());
		System.out.println("代理方法返回类型"+method.getReturnType());
		String str = annotation.value();
		System.out.println(str);
		userDao.query();
		//Class<?> returnType =method.getReturnType();
		System.out.println("jdk代理完成");
		return null;
	}

	public static class UserDaoImpl implements UserDao {
		@Override
		public void query() {
			System.out.println("数据库查询完毕");
		}
	}
}
